package kr.cafedev.hangul;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
	
	

	
	 final static String TAG = "MainActivity";
	 private static final String DATABASE_NAME = "base.db";
	 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        //ImageButton btn = (ImageButton)findViewById(R.id.btnNew);
        ImageView category1 = (ImageView)findViewById(R.id.id_play_hangul); 
        ImageView category2 = (ImageView)findViewById(R.id.id_play_number);
        ImageView category3 = (ImageView)findViewById(R.id.id_play_family);

        
        category1.setOnClickListener(listener);
        category2.setOnClickListener(listener);
        category3.setOnClickListener(listener);

        copyDatabase();
    }
    

    public void copyDatabase(){

    	File folder = new File("/data/data/kr.cafedev.hangul/databases/");
    	folder.mkdirs();
    	
    	
        File file = new File("/data/data/kr.cafedev.hangul/databases/base.db");
        
        if(file.exists()){
        	Log.e(TAG,"database exists");
        	return;
        }
        
        		AssetManager assetManager = getResources().getAssets();
        		
        		try{
	        		InputStream is = assetManager.open("database/base.db", AssetManager.ACCESS_BUFFER);
	        		OutputStream out = new FileOutputStream(file);
	        		writeFile(is, out);
	        		out.close();
	        		is.close();
        		}catch (Exception e){
        			e.printStackTrace();
        		}

    }
    public void writeFile(InputStream is, OutputStream os) throws IOException
    {
         int c = 0;
         while((c = is.read()) != -1)
             os.write(c);
         os.flush();
    }  
/*	private void prepareDialog() {
		Context mContext = getApplicationContext();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.selectdialog, null);       
        dialog=new Dialog(MainActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(layout);
        //dialog.setTitle("Choice");
        
       
        ImageView category1  = (ImageView)layout.findViewById(R.id.ImageView1);
        ImageView category2  = (ImageView)layout.findViewById(R.id.ImageView2);
        ImageView category3  = (ImageView)layout.findViewById(R.id.ImageView3);
        ImageView category4  = (ImageView)layout.findViewById(R.id.ImageView4);
        category1.setOnClickListener(listener);
        category2.setOnClickListener(listener);
        category3.setOnClickListener(listener);
        category4.setOnClickListener(listener);
		
	}*/

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
			case R.id.id_play_hangul:
				startActivity(new Intent(MainActivity.this, Category1.class));
				break;
			case R.id.id_play_number:
				startActivity(new Intent(MainActivity.this, Category2.class));
				break;
			case R.id.id_play_family:
				startActivity(new Intent(MainActivity.this, Category3.class));
				break;

				

			}
		}
	};


	@Override
	protected void onDestroy() {
		Log.e(TAG,"onDestory");
		
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Log.e(TAG,"onPause");
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Log.e(TAG,"onRestart");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.e(TAG,"onResume");
		super.onResume();
	}

	@Override
	protected void onStart() {
		Log.e(TAG,"onStart");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.e(TAG,"onStop");
		super.onStop();
	}



}
